<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>字典明细管理</title>
    <#-- 使用相对当前模板文件的路径 再去找另一个模板文件 -->
    <#include "/common/link.ftl">


</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--定义一个变量  菜单回显-->
    <#assign currentMenu="systemDictionaryItem"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>字典明细管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <div class="row" style="margin:20px">
                    <div class="col-xs-2">
                        <div class="panel panel-default">
                            <div class="panel-heading">字典目录</div>
                            <div class="panel-body">
                                <div class="list-group" id="dic">
                                    <#list dictionaries as dic>
                                        <a data-id="${dic.id}" href="/systemDictionaryItem/list?typeId=${dic.id}" class="list-group-item">${dic.title}</a>
                                    </#list>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-10">
                        <!--高级查询 每次点下页都会提交表单--->
                        <form class="form-inline" id="searchForm" action="/systemDictionaryItem/list" method="post">
                            <input type="hidden" name="currentPage" id="currentPage" value="1">
                            <input type="hidden" name="typeId" id="typeId" value="${qo.typeId}">
                            <input type="hidden" name="parentId" id="parentId" value="${qo.parentId}">
                            <a href="#" class="btn btn-success btn-input" style="margin: 10px">
                                <span class="glyphicon glyphicon-plus"></span> 添加
                            </a>
                        </form>
                        <!--编写内容-->
                        <div class="box-body table-responsive no-padding ">
                            <table class="table table-hover table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>编号</th>
                                        <th>字典明细标题</th>
                                        <th>字典明细序号</th>
                                        <th>上级明细</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <#list pageInfo.list as sdi>
                                        <tr>
                                            <td>${sdi_index+1}</td>
                                            <td>
                                                <a href="/systemDictionaryItem/list?parentId=${sdi.id}">${sdi.title}</a>
                                            </td>
                                            <td>${sdi.sequence}</td>
                                            <#--有些没有上级明细的,会返回null,给他设置默认值无-->
                                            <td>${sdi.parent.title!"无"}</td>
                                            <td>
                                                <a class="btn btn-info btn-xs btn-input" data-json='${sdi.toJson()}'>
                                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                                </a>
                                            </td>
                                        </tr>
                                    </#list>
                                </tbody>
                            </table>
                            <#include "/common/page.ftl" >
                        </div>
                    </div>
                </div>

            </div>
        </section>
    </div>
    <#include "/common/footer.ftl" >
</div>


<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <form class="form-horizontal" action="/systemDictionaryItem/saveOrUpdate" method="post" id="editForm">
                <div class="modal-body">
                        <input type="hidden" name="id">
                        <#--用表选择并传递给后端的type.id-->
                        <div class="form-group" style="margin-top: 10px;">
                         <label for="name" class="col-sm-3 control-label">所属目录：</label>
                            <div class="col-sm-6">
                                <select class="form-control" name="type.id">
                                    <option value="">请选择所属目录</option>
                                    <#list dictionaries as d>
                                        <option value="${d.id}">${d.title}</option>
                                    </#list>
                                </select>
                            </div>
                        </div>
                        <#--用来联动上级明细,不做提交-->
                        <div class="form-group" style="margin-top: 10px;">
                            <label for="name" class="col-sm-3 control-label">上级目录：</label>
                            <div class="col-sm-6">
                                <select class="form-control" id="typeSelect">
                                    <option value="">无</option>
                                    <#list dictionaries as d>
                                        <option value="${d.id}">${d.title}</option>
                                    </#list>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" style="margin-top: 10px;">
                            <label  class="col-sm-3 control-label">上级明细：</label>
                            <div class="col-sm-6">
                                <select class="form-control" name="parent.id" id="parentSelect">
                                    <option value="">无</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" style="margin-top: 10px;">
                            <label  class="col-sm-3 control-label">明细标题：</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="title" placeholder="请输入字典明细编码">
                            </div>
                        </div>
                        <div class="form-group" style="margin-top: 10px;">
                            <label class="col-sm-3 control-label">明细序号：</label>
                            <div class="col-sm-6">
                                <input type="number" class="form-control" name="sequence" placeholder="请输入字典明细序号">
                            </div>
                        </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    //注意此处不能用js的if,因为js无法通过${qo.typeId}获取值
    <#if qo.typeId??>
    $("a[data-id=${qo.typeId}]").addClass('active');
    </#if>


    //点击新增/编辑
    $(".btn-input").click(function () {
        //需要注意editForm是模态框里面的表单id  editModal是整个模态框本身的id
        //同时设置input和select标签里的内容为空
        $('#editForm input,#editForm select').val('');
        let sd = $(this).data('json');
        if(sd){
            $('#editForm input[name=id]').val(sd.id);
            $('#editForm input[name=title]').val(sd.title);
            $('#editForm input[name=sequence]').val(sd.sequence);
            //由于使用了.,需要用引号
            $('#editForm select[name="type.id"]').val(sd.typeId);

            //由于上级明细无法回显,因为上级明细是通过改变事件显示出来的
            //虽然可以再发一次ajax请求实现但很麻烦
            //因此可以直接根据data-json里的数据直接拼接option
            let src = '<option value="' + sd.parentId + '">' + sd.parentTitle + '</option>';
            //此时覆盖了原来的所有option,只剩一个刚才拼接的option,可以不用设置val了
            $("#parentSelect").html(src);
            /*$('#editForm select[name="parent.id"]').val(sd.parentId);*/
        }
        //展示模态框
        $('#editModal').modal('show');
    });

    //将模态框的提交改为ajax提交 然后刷新页面,这样提交以后就可以保存原有的回显值
    $('#editModal').ajaxForm(function (data) {
        if(data.success){
            //如果成功给出一个框提示
            Swal.fire(
                '成功',
                '',
                'success'
            ).then((result) => {
                if(result.value){
                    //如果点击了确定执行以下操作
                    //刷新当前页面
                    window.location.reload();
                }
            });
        }else {
            Swal.fire(
                data.msg,
                '',
                'error'
            )
        }
    });

    //上级明细-二级联动,点击目录,再上级明细联动
    $('#typeSelect').change(function () {
        let val = $(this).val();
        $.get("/systemDictionaryItem/parentSelect",{id:val},function (data) {
            //发送并查询数据,并把每个数据用字符串拼接成option接在原来的select后面
            let str = '';
            $.each(data,function (i,ele) {
                str += "<option value="+ele.id+">"+ele.title+"</option>"
            });
            //把字符串拼接到select标签后面,使用html可以覆盖原来的option标签
            $('#parentSelect').html(str);
        });
    });
</script>
</body>
</html>
